//
//  WJ_TJDY_VC.m
//  WJBSApp
//
//  Created by dllo on 16/7/16.
//  Copyright © 2016年 王景伟. All rights reserved.
//
//  注意这个9 是theme-id
// tags

//  搜索的网址: 明天写吧 写数据库  上面的东西就不写了  需要添加订阅
// 头像:http://api.budejie.com/api/api_open.php?&a=theme_info&c=topic&theme_id=3096  // WJMeModel 可以解析
// 关注的人:http://api.budejie.com/api/api_open.php?a=theme_users&c=topic&theme_id=9
// 最热:http://s.budejie.com/topic/tag-topic/9/hot/bs0315-iphone-4.3/0-20.json
// 最新:http://s.budejie.com/topic/tag-topic/9/new/bs0315-iphone-4.3/0-20.json

// 我的订阅进去的页面
#import "WJ_TJDY_VC.h"
#import "WJMeModel.h"
#import "WJMainModel.h"
#import "WJMainTableViewCell.h"
#import "WJEssAndNewPLVC.h"
#import "WJDataManager.h"


@interface WJ_TJDY_VC ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain) WJBaseButton *rightBarBut;

@property (nonatomic,retain) WJBaseImageView *headerImage;

@property (nonatomic,retain) WJBaseButton *rightBut;

@property (nonatomic,retain) UILabel *rightLable;

@property (nonatomic,retain) WJBaseButton *leftBut;

@property (nonatomic,retain) UILabel *leftLable;

@property (nonatomic,retain) UIScrollView *scrollView;

@property (nonatomic,retain) WJMainTableView *rightTableView;

@property (nonatomic,retain) NSMutableArray *rightArray;

@property (nonatomic,retain) WJMainTableView *leftTableView;

@property (nonatomic,retain) NSMutableArray *leftArray;

@property (nonatomic,retain) NSNumber *leftNp;

@property (nonatomic,retain) NSNumber *rightNp;

@property (nonatomic,retain) WJMeModel *model;

@property (nonatomic,assign) BOOL isExist;

@property (nonatomic,assign) NSInteger presentTag;

@end

@implementation WJ_TJDY_VC


- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self createConfig];
    
    [self createView];
}


- (void)createConfig {
    self.leftArray = [NSMutableArray array];
    self.rightArray = [NSMutableArray array];
    
    // 影藏系统的返回按钮
    self.navigationItem.hidesBackButton = YES;
    
    self.view.backgroundColor = BSWhiteColor;
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationButtonReturn.png" hightImage:@"navigationButtonReturnClick.png" target:self action:@selector(tjLeftAction)];
    
    self.rightBarBut = [WJBaseButton buttonWithTitle:nil frame:CGRectMake(0, 0, 60, 30) target:self action:@selector(rightButtonItemAction:)];
    self.rightBarBut.titleLabel.font = [UIFont systemFontOfSize:13];
    
    
    BOOL  exist = [[WJDataManager shareFMDB] isExistTheme_id:self.theme_id.integerValue];
    
    if (exist) {
        [self.rightBarBut setTitle:@"取消订阅" forState:0];
        [self.rightBarBut setTitleColor:BSGrayColor forState:0];
    }else{
        [self.rightBarBut setTitle:@"+订阅" forState:0];
        [self.rightBarBut setTitleColor:BSRedColor forState:0];
    }
    
    self.rightBarBut.layer.cornerRadius = 3;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:_rightBarBut];
}

// 放回上一个界面
- (void)tjLeftAction {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma Mark - 收藏的东西
- (void)rightButtonItemAction:(WJBaseButton *)button {
    NSString *str = button.titleLabel.text;
    if ([str isEqualToString:@"取消订阅"]) {
        [self.rightBarBut setTitle:@"+订阅" forState:0];
        [self.rightBarBut setTitleColor:BSRedColor forState:0];
        
        
        [[WJDataManager shareFMDB] deleteFromFirWithTheme_id:self.theme_id.integerValue];
        
        NSString *url = [NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?&a=theme_info&c=topic&theme_id=%ld",(long)self.theme_id];
        
        [WJNetTool GET:url body:nil header:nil response:WJJSON success:^(id resuposeObject) {
            
            NSDictionary *dic = resuposeObject[@"info"];
            
            NSString *Theme_id = dic[@"theme_id"];
            NSString *Sub_number = dic[@"sub_number"];
            NSString *Post_num = dic[@"post_number"];
            
            [[WJDataManager shareFMDB] insertToSecTheme_id:Theme_id.integerValue Image_list:dic[@"image_list"] Sub_number:Sub_number.integerValue Post_num:Post_num.integerValue Theme_name:dic[@"theme_name"]];
            
        } failure:^(NSError *error) {}];
        
    }else{
        [self.rightBarBut setTitle:@"取消订阅" forState:0];
        [self.rightBarBut setTitleColor:BSGrayColor forState:0];
        
        [[WJDataManager shareFMDB] deleteFromSecTheme_id:self.theme_id.integerValue];

        NSString *url = [NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?&a=theme_info&c=topic&theme_id=%ld",(long)self.theme_id];
        
        [WJNetTool GET:url body:nil header:nil response:WJJSON success:^(id resuposeObject) {
            
            NSDictionary *dic = resuposeObject[@"info"];
            
            NSString *Theme_id = dic[@"theme_id"];
            NSString *Sub_number = dic[@"sub_number"];
            NSString *Post_num = dic[@"post_number"];
            
            [[WJDataManager shareFMDB] insertToFirTheme_id:Theme_id.integerValue Image_list:dic[@"image_list"] Sub_number:Sub_number.integerValue Post_num:Post_num.integerValue Theme_name:dic[@"theme_name"]];
            
        } failure:^(NSError *error) {}];        
    }
}


- (void)createView {
    self.leftBut = [WJBaseButton buttonWithTitle:@"最热" frame:CGRectMake(0, 0, WIDTH / 2, 46) target:self action:@selector(leftAction:)];
    [self.view addSubview:self.leftBut];
    [self.leftBut setTitleColor:BSRedColor forState:0];
    self.leftLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 47, WIDTH / 2, 2)];
    self.leftLable.backgroundColor = BSRedColor;
    [self.view addSubview:self.leftLable];
    
    self.rightBut = [WJBaseButton buttonWithTitle:@"最新" frame:CGRectMake(WIDTH / 2, 0, WIDTH / 2, 46) target:self action:@selector(rightAction:)];
    [self.view addSubview:self.rightBut];
    [self.rightBut setTitleColor:BSGrayColor forState:0];
    self.rightLable = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH / 2, 47, WIDTH / 2, 2)];
    self.rightLable.backgroundColor = BSRedColor;
    self.rightLable.hidden = YES;
    [self.view addSubview:self.rightLable];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 48, WIDTH, 1)];
    lable.backgroundColor = BSLightGrayColor;
    [self.view addSubview:lable];

    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 48, WIDTH, BSViewH - 48)];
    self.scrollView.backgroundColor = BSWhiteColor;
    self.scrollView.contentSize = CGSizeMake(WIDTH * 2, 0);
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    [self.view addSubview:self.scrollView];

    self.leftTableView = [WJMainTableView mainTableViewWithFrame:CGRectMake(0, 0, WIDTH, BSViewH - 48) controller:self];
    self.leftTableView.tagEnabled = NO;
    self.leftTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(createleftData)];
    self.leftTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self loadLeftMoreData:self.leftNp];
    }];
    [self.scrollView addSubview:self.leftTableView];

    
    self.rightTableView = [WJMainTableView mainTableViewWithFrame:CGRectMake(WIDTH, 0, WIDTH, BSViewH - 48) controller:self];
    self.rightTableView.tagEnabled = NO;
    self.rightTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(createRightData)];
    self.rightTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self loadRightMoreData:self.rightNp];
    }];
    [self.scrollView addSubview:self.rightTableView];
    
    [self.leftTableView.mj_header beginRefreshing];
    
    [self.rightTableView.mj_header beginRefreshing];
}


- (void)leftAction:(WJBaseButton *)button {
    [self.leftBut setTitleColor:BSRedColor forState:0];
    self.leftLable.hidden = NO;
    [self.rightBut setTitleColor:BSGrayColor forState:0];
    self.rightLable.hidden = YES;
    self.scrollView.contentOffset = CGPointMake(0, 0);
    
}

- (void)rightAction:(WJBaseButton *)button {
    [self.leftBut setTitleColor:BSGrayColor forState:0];
    self.leftLable.hidden = YES;
    [self.rightBut setTitleColor:BSRedColor forState:0];
    self.rightLable.hidden = NO;
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if ([scrollView isEqual:self.scrollView]) {
        NSInteger temp = scrollView.contentOffset.x / WIDTH;
        if (temp == 0) {
            [self.leftBut setTitleColor:BSRedColor forState:0];
            self.leftLable.hidden = NO;
            [self.rightBut setTitleColor:BSGrayColor forState:0];
            self.rightLable.hidden = YES;
        }else{
            [self.leftBut setTitleColor:BSGrayColor forState:0];
            self.leftLable.hidden = YES;
            [self.rightBut setTitleColor:BSRedColor forState:0];
            self.rightLable.hidden = NO;
        }
    }
}


- (void)createleftData {
    NSString *leftUrl = [NSString stringWithFormat:@"http://s.budejie.com/topic/tag-topic/%@/hot/bs0315-iphone-4.3/0-20.json",self.theme_id];
    
    [WJNetTool GET:leftUrl body:nil header:nil response:WJJSON success:^(id resuposeObject) {
        NSDictionary *dic = resuposeObject;
        if (dic[@"info"][@"np"] != nil) {
            self.leftNp = dic[@"info"][@"np"];
        }
        [self.leftArray removeAllObjects];
        NSArray *array = [WJBSTool getSafeArray:dic[@"list"]];
        for (NSDictionary *dic in array) {
            WJMainModel *model = [WJMainModel modelWithJSON:dic];
            WJMianLayout *layout = [[WJMianLayout alloc]initWithModel:model];
            [self.leftArray addObject:layout];
        }
        self.leftTableView.dataArr = self.leftArray;
        [self.leftTableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        [self.leftTableView.mj_header endRefreshing];
    }];
}

- (void)loadLeftMoreData:(NSNumber *)np {
    NSString *leftUrl = [NSString stringWithFormat:@"http://s.budejie.com/topic/tag-topic/%ld/hot/bs0315-iphone-4.3/%@-20.json",(long)self.theme_id,np];
    
    [WJNetTool GET:leftUrl body:nil header:nil response:WJJSON success:^(id resuposeObject) {
        NSDictionary *dic = resuposeObject;
        self.leftNp = dic[@"info"][@"np"];
        NSArray *array = [WJBSTool getSafeArray:dic[@"list"]];
        for (NSDictionary *dic in array) {
            WJMainModel *model = [WJMainModel modelWithJSON:dic];
            WJMianLayout *layout = [[WJMianLayout alloc]initWithModel:model];
            [self.leftArray addObject:layout];
        }
        self.leftTableView.dataArr = self.leftArray;
        [self.leftTableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        [self.leftTableView.mj_footer endRefreshing];
    }];
}

- (void)createRightData {
    
    NSString *rightUrl = [NSString stringWithFormat:@"http://s.budejie.com/topic/tag-topic/%ld/new/bs0315-iphone-4.3/0-20.json",(long)self.theme_id];
    
    [WJNetTool GET:rightUrl body:nil header:nil response:WJJSON success:^(id resuposeObject) {
        NSDictionary *dic = resuposeObject;
        if (dic[@"info"][@"np"] != nil) {
            self.rightNp = dic[@"info"][@"np"];
        }
        [self.rightArray removeAllObjects];
        NSArray *array = [WJBSTool getSafeArray:dic[@"list"]];
        for (NSDictionary *dic in array) {
            WJMainModel *model = [WJMainModel modelWithJSON:dic];
            WJMianLayout *layout = [[WJMianLayout alloc]initWithModel:model];
            [self.rightArray addObject:layout];
        }
        self.rightTableView.dataArr = self.rightArray;
        [self.rightTableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        [self.rightTableView.mj_header endRefreshing];
    }];
}


- (void)loadRightMoreData:(NSNumber *)np {
    
    NSString *rightUrl = [NSString stringWithFormat:@"http://s.budejie.com/topic/tag-topic/%ld/new/bs0315-iphone-4.3/%@-20.json",(long)self.theme_id,np];
    
    [WJNetTool GET:rightUrl body:nil header:nil response:WJJSON success:^(id resuposeObject) {
        NSDictionary *dic = resuposeObject;
        self.rightNp = dic[@"info"][@"np"];
        NSArray *array = [WJBSTool getSafeArray:dic[@"list"]];
        for (NSDictionary *dic in array) {
            WJMainModel *model = [WJMainModel modelWithJSON:dic];
            WJMianLayout *layout = [[WJMianLayout alloc]initWithModel:model];
            [self.rightArray addObject:layout];
        }
        self.rightTableView.dataArr = self.rightArray;
        [self.rightTableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        [self.rightTableView.mj_footer endRefreshing];
    }];
}
@end
